# models/llm.py
from sqlalchemy import Column, Integer, String, Text, DateTime, ForeignKey, Enum, JSON
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
import enum

from dao.postgresql_connect import Base

class ModelModality(enum.Enum):
    """模型模态类型枚举"""
    TEXT_ONLY = "text_only"           # 仅文本
    TEXT_IMAGE = "text_image"         # 文本+图像
    TEXT_VIDEO = "text_video"         # 文本+视频
    TEXT_AUDIO = "text_audio"         # 文本+音频
    MULTIMODAL = "multimodal"         # 多模态（支持多种输入输出）

class ModelStatus(enum.Enum):
    """模型状态枚举"""
    ACTIVE = "active"
    INACTIVE = "inactive"
    DEPRECATED = "deprecated"

class LLMModel(Base):
    """开源大模型信息表"""
    __tablename__ = "llm_models"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), nullable=False, comment="模型显示名称")
    model_id = Column(String(100), nullable=False, unique=True, comment="模型ID")
    provider = Column(String(50), nullable=False, comment="模型提供商")
    modality = Column(Enum(ModelModality), nullable=False, comment="模型模态类型")
    base_url = Column(String(255), nullable=False, comment="API基础URL")
    api_key = Column(String(255), nullable=False, comment="API密钥")
    status = Column(Enum(ModelStatus), default=ModelStatus.ACTIVE, comment="模型状态")
    description = Column(Text, nullable=True, comment="模型描述")
    config = Column(JSON, nullable=True, comment="模型配置参数")

    # 审计字段
    created_at = Column(DateTime(timezone=True), server_default=func.now(), comment="创建时间")
    updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now(), comment="更新时间")
    created_by = Column(String(36), ForeignKey("users.id"), comment="创建者ID")
    updated_by = Column(String(36), ForeignKey("users.id"), comment="更新者ID")

    # 关联关系
    creator = relationship("User", foreign_keys=[created_by], backref="created_models")
    updater = relationship("User", foreign_keys=[updated_by], backref="updated_models")

    def __repr__(self):
        return f"<LLMModel {self.name} ({self.model_id})>"